
const app = getApp()
import api from '../../utils/api'
const util = require('../../utils/util.js')

Page({
  data: {
    food: {},
    list:[],
    cartHidden:true,
    goods:[],
    carArray: [], //购物车
    carHalfArray: [], //小份购物车
  },
  onLoad: function () {
    var _this = this;
    var food = app.globalData.food;
    var goods = app.globalData.stoves;
    var cartGoods = app.globalData.cartGoods;
    var cartHalfGoods = app.globalData.cartHalfGoods;
    this.setData({
      food: food,
      goods: goods,
      carArray: cartGoods,
      carHalfArray: cartHalfGoods
    })
    let param = { "id": food.id };
    api.POST({
      url: 'nutrition.action',
      params: param,
      success: function (res) {
        let success = res.data.success;
        if (success == "true") {
          const datas = res.data.data;
          
          const list = [];
          for (var i = 0; i < datas.length; i++) {
            Object.keys(datas[i]).forEach((key, value) => {
              let _obj = {
                label: key,
                value: datas[i][key]
              }
              list.push(_obj);
            });
          }
          _this.setData({
            list: list
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
            duration: 5000
          })
        }
      },
      fail: function () {
        wx.showToast({
          icon: 'none',
          title: '请求失败',
          duration: 5000
        })
      },
    })
  },
  cancel: function () {
    this.setData({
      cartHidden: true
    });
  },
  showAddCart(e) {
    var food = this.data.food;
    if (!food["count"]) {
      food.count = 0;
    }
    if (!food["xfCount"]) {
      food.xfCount = 0;
    }
    this.setData({
      cartHidden: false,
      food: food
    })
  },
  //小份添加到购物车
  addSmalCart(e) {
    var tabIndex = this.data.food.tabIndex;
    var mealIndex = this.data.food.mealIndex;
    var foodIndex = this.data.food.foodIndex;
    if (this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex]["countSum"]) {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].countSum++;
    } else {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].countSum = 1;
    }
    if (this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex]["xfCount"]) {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].xfCount++;
    } else {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].xfCount = 1;
    }
    if (this.data.food["countSum"]) {
      this.data.food.countSum++;
    } else {
      this.data.food.countSum = 1;
    }


    var id = this.data.food.id;
    var num = 1;
    var xfPrice = this.data.food.xfPrice;
    var cook_img = this.data.food.cook_img;
    var name = this.data.food.name;
    var meals = this.data.food.meals;
    var half_state = this.data.food.half_state;
    var obj = {
      id: id,
      num: num,
      name: name + "--小份",
      price: xfPrice,
      meals: meals,
      cook_img: cook_img,
      checked: true,
      sfxf: 'y',
      tabIndex: tabIndex,
      mealIndex: mealIndex,
      foodIndex: foodIndex,
      half_state
    };
    var carList = this.data.carHalfArray;
    carList.push(obj);

    this.data.food.xfCount++;
    this.setData({
      dataCount: this.data.dataCount + 1,
      carHalfArray: carList,
      goods: this.data.goods,
      food: this.data.food,
    })

    app.globalData.cartHalfGoods = carList;
    app.globalData.stoves = this.data.goods;
  },
  //大份添加到购物车
  addBigCart(e) {
    var tabIndex = this.data.food.tabIndex;
    var mealIndex = this.data.food.mealIndex;
    var foodIndex = this.data.food.foodIndex;

    if (this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex]["countSum"]) {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].countSum++;
    } else {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].countSum = 1;
    }

    if (this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex]["count"]) {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].count++;
    } else {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].count = 1;
    }
    this.data.food.count++;
    if (this.data.food["countSum"]) {
      this.data.food.countSum++;
    } else {
      this.data.food.countSum = 1;
    }

    var id = this.data.food.id;
    var num = this.data.food.count;
    var price = this.data.food.price;
    var cook_img = this.data.food.cook_img;
    var name = this.data.food.name;
    var meals = this.data.food.meals;
    var half_state = this.data.food.half_state;
    var obj = {
      id: id,
      num: num,
      name: name,
      price: price,
      meals: meals,
      cook_img: cook_img,
      checked: true,
      sfxf: 'n',
      tabIndex: tabIndex,
      mealIndex: mealIndex,
      foodIndex: foodIndex,
      half_state: half_state
    };
    var carList = this.data.carArray.filter(item => item.id != id)
    carList.push(obj)

    this.setData({
      dataCount: this.data.dataCount + 1,
      carArray: carList,
      goods: this.data.goods,
      food: this.data.food
    })

    app.globalData.cartGoods = carList;
    app.globalData.stoves = this.data.goods;
  },
  //添加到购物车
  addCart(e) {
    var tabIndex = this.data.food.tabIndex;
    var mealIndex = this.data.food.mealIndex;
    var foodIndex = this.data.food.foodIndex;

    if (this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex]["Count"]) {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].Count++;
    } else {
      this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].Count = 1;
    }

    if (this.data.food["Count"]){
      this.data.food.Count++;
    }else{
      this.data.food.Count = 1;
    }

    var id = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].id;
    var num = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].Count;
    var price = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].price;
    var cook_img = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].cook_img;
    var name = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].name;
    var meals = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].meals;
    var half_state = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].half_state;
    var obj = {
      id: id,
      num: num,
      name: name,
      price: price,
      meals: meals,
      cook_img: cook_img,
      checked: true,
      sfxf: 'n',
      tabIndex: tabIndex,
      mealIndex: mealIndex,
      foodIndex: foodIndex,
      half_state
    };
    var carList = this.data.carArray.filter(item => item.id != id)
    carList.push(obj)
    this.setData({
      carArray: carList,
      goods: this.data.goods,
      food:this.data.food
    })

    app.globalData.cartGoods = carList;
    app.globalData.stoves = this.data.goods;
  },

  //小份移除商品
  decreaseSmalCart: function (e) {
    var tabIndex = this.data.food.tabIndex;
    var mealIndex = this.data.food.mealIndex;
    var foodIndex = this.data.food.foodIndex;
    this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].countSum--;
    this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].xfCount--;

    var id = this.data.food.id;

    var carList = this.data.carHalfArray;
    for (var m = 0; m < carList.length; m++) {
      if (carList[m].id == id) {
        carList.splice(m, 1); // a需要删除的位置,b删除几个
        break;
      }
    }

    this.data.food.xfCount--;
    this.data.food.countSum--;
    this.setData({
      dataCount: this.data.dataCount - 1,
      carHalfArray: carList,
      goods: this.data.goods,
      food: this.data.food,
    })

    app.globalData.cartHalfGoods = carList;
    app.globalData.stoves = this.data.goods;
  },
  //大份移除商品
  decreaseBigCart: function (e) {
    var tabIndex = this.data.food.tabIndex;
    var mealIndex = this.data.food.mealIndex;
    var foodIndex = this.data.food.foodIndex;

    this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].countSum--;
    this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].count--;

    this.data.food.count--;
    this.data.food.countSum--;
    var id = this.data.food.id;
    var num = this.data.food.count;
    var price = this.data.food.price;
    var cook_img = this.data.food.cook_img;
    var name = this.data.food.name;
    var meals = this.data.food.meals;
    var half_state = this.data.food.half_state;
    var obj = {
      id: id,
      num: num,
      name: name,
      price: price,
      meals: meals,
      cook_img: cook_img,
      checked: true,
      sfxf: 'n',
      tabIndex: tabIndex,
      mealIndex: mealIndex,
      foodIndex: foodIndex,
      half_state: half_state
    };
    var carList = this.data.carArray.filter(item => item.id != id);
    carList.push(obj);
    for (var m = 0; m < carList.length; m++) {
      if (carList[m].num == 0) {
        carList.splice(m, 1); // a需要删除的位置,b删除几个
      }
    }
    this.setData({
      dataCount: this.data.dataCount - 1,
      carArray: carList,
      goods: this.data.goods,
      food: this.data.food
    })

    app.globalData.cartGoods = carList;
    app.globalData.stoves = this.data.goods;
  },
  //移除商品
  decreaseCart: function (e) {
    var tabIndex = this.data.food.tabIndex;
    var mealIndex = this.data.food.mealIndex;
    var foodIndex = this.data.food.foodIndex;

    this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].Count--;
    this.data.food.Count--;
    var id = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].id;
    var num = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].Count;
    var price = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].price;
    var cook_img = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].cook_img;
    var name = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].name;
    var meals = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].meals;
    var half_state = this.data.goods[tabIndex].meals[mealIndex].recipes[foodIndex].half_state;
    var obj = {
      id: id,
      num: num,
      name: name,
      price: price,
      meals: meals,
      cook_img: cook_img,
      checked: true,
      sfxf: 'n',
      tabIndex: tabIndex,
      mealIndex: mealIndex,
      foodIndex: foodIndex,
      half_state: half_state
    };

    var carList = this.data.carArray.filter(item => item.id != id);
    carList.push(obj);
    for (var m = 0; m < carList.length; m++) {
      if (carList[m].num == 0) {
        carList.splice(m, 1); // a需要删除的位置,b删除几个
      }
    }
    this.setData({
      dataCount: this.data.dataCount - 1,
      carArray: carList,
      goods: this.data.goods,
      food:this.data.food
    })

    app.globalData.cartGoods = carList;
    app.globalData.stoves = this.data.goods;
  },
})
